/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl;

import cz.fi.muni.diplomka.common.Squad;
import cz.muni.fi.diplomka.datalayer.dao.CityArmyDao;
import cz.muni.fi.diplomka.datalayer.daoimpl.CityArmyDaoImpl;
import cz.muni.fi.diplomka.datalayer.entity.CityArmyEntity;
import cz.muni.fi.diplomka.servicelayer.dto.CityArmy;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityArmyManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityUnitsManager;
import java.util.ArrayList;
import java.util.List;
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;

/**
 *
 * @author Jakub Kutil
 */
public class CityArmyManagerImpl implements CityArmyManager {

    private CityArmyDao cityArmyDao = new CityArmyDaoImpl();
    private CityUnitsManager cityUnitsManager = new CityUnitsManagerImpl();
    private Mapper mapper = new DozerBeanMapper();

    @Override
    public void addArmy(CityArmy army) {
        if (army == null) {
            throw new IllegalArgumentException("Army can not be null.");
        }
        cityArmyDao.addArmy(mapper.map(army, CityArmyEntity.class));
        
        for(Squad squad : army.getArmy().getSquads()){
            cityUnitsManager.lowerUnitCount(army.getCityId(), squad.getUnit().getName(), (int) squad.getCount());
        }
    }

    @Override
    public void editArmy(CityArmy army) {
        if (army == null) {
            throw new IllegalArgumentException("Army can not be null.");
        }
        cityArmyDao.editArmy(mapper.map(army, CityArmyEntity.class));
    }

    @Override
    public void deleteArmy(CityArmy army) {
        if (army == null) {
            throw new IllegalArgumentException("Id can not be null.");
        }
        
        for(Squad squad : army.getArmy().getSquads()){
            cityUnitsManager.increaseUnitCount(army.getCityId(),  squad.getUnit().getName(), (int) squad.getCount());
        }
        
        cityArmyDao.deleteArmy(army.getId());
    }
    
    @Override
    public void sendArmy(CityArmy army) {
        if (army == null) {
            throw new IllegalArgumentException("Id can not be null.");
        }
                  
        cityArmyDao.deleteArmy(army.getId());
    }

    @Override
    public List<CityArmy> getArmiesInCity(Long cityId) {
        List<CityArmy> cityArmies = new ArrayList<>();
        if (cityId == null) {
            throw new IllegalArgumentException("CityId can not be null.");
        }
        for (CityArmyEntity cityArmyEntity : cityArmyDao.getArmiesInCity(cityId)) {
            cityArmies.add(mapper.map(cityArmyEntity, CityArmy.class));
        }
        return cityArmies;
    }
    
    @Override
    public List<CityArmy> getAwayArmiesFromCity(Long cityId) {
        List<CityArmy> cityArmies = new ArrayList<>();
        if (cityId == null) {
            throw new IllegalArgumentException("CityId can not be null.");
        }
        for (CityArmyEntity cityArmyEntity : cityArmyDao.getAwayArmiesFromCity(cityId)) {
            cityArmies.add(mapper.map(cityArmyEntity, CityArmy.class));
        }
        return cityArmies;
    }

    @Override
    public CityArmy getArmyOnCityWalls(Long cityId) {
        if (cityId == null) {
            throw new IllegalArgumentException("CityId can not be null.");
        }
        if(cityArmyDao.getArmyOnWallsInCity(cityId) == null){
            return null;
        }
        CityArmy cityArmy = mapper.map(cityArmyDao.getArmyOnWallsInCity(cityId), CityArmy.class);

        return cityArmy;
    }
}
